const dbUtils = require('../utils/db-util');

const chat = {

	/**
	 * 发送用户私聊信息
	 * @param  {object} model 消息数据模型
	 * @return {object}       mysql执行结果
	 */
	async sendMessage(model) {
		let result = await dbUtils.insertData('message_user', model);
		return result;
	},

	/**
	 * 获取用户私聊信息
	 * @param  {object} model 消息数据模型
	 * @return {object}       mysql执行结果
	 */
	async getMessage(model) {
		let _sql = `
			select * from message_user 
				where from_user = ${model.from_user} and to_user = ${model.to_user}
				or from_user = ${model.to_user} and to_user = ${model.from_user} 
				order by time desc limit 5`;
		let result = await dbUtils.query(_sql);
		if(Array.isArray(result) && result.length > 0) {
			result = result;
		} else {
			result = null;
		}
		return result;
	},
};

module.exports = chat;